#include <vector>
#include <set>
#include <iostream>
#include <unordered_map>
using namespace std;

// 有问题待排查
int myhIndex(vector<int>& citations) {
    int overIndex = 0; int zero = 0;
    for(int i = 0; i < citations.size(); i++){
        if(citations[i] <= citations.size()){
            citations[i] == 0 ? zero++ : citations[citations[i]-1] += 1001;
        }else if(citations[i] > citations.size() && citations[i] <= 1000){
            overIndex++;
        }else if(citations[i] > 1000){
            if(citations[i]%1001 > citations.size()){
                overIndex++;
            }else{
                citations[i]%1001 == 0 ? zero++ : citations[citations[i]%1001-1] += 1001;
            }
        }
    }
    for(int i = citations.size()-1; i >= 0; i--){
        if(citations[i] > 1000){
            overIndex = overIndex + citations[i]/1001;
        }
        if(overIndex  >= i+1){
            return i+1;
        }
    }
    return 0;
}

// 和我的思想一样，但是多用了一个数组，代码简单但是空间复杂度高
int hIndex(vector<int>& citations) {
    int n = citations.size(), tot = 0;
    vector<int> counter(n + 1);
    for (int i = 0; i < n; i++) {
        if (citations[i] >= n) {
            counter[n]++;
        } else {
            counter[citations[i]]++;
        }
    }
    for (int i = n; i >= 0; i--) {
        tot += counter[i];
        if (tot >= i) {
            return i;
        }
    }
    return 0;
}

int main(){
    vector<int> citations = { 4,4,0,0 };
    cout << hIndex(citations) << endl;
    return 0;
}